package com.xbongbong.saas.help;

import com.xbongbong.parent.data.FormDataEsService;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.elasticsearch.util.EsUtil;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.FlowStatusEnum;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.constant.BusinessConstant;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.form.pojo.dto.EsDataDTO;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.enums.InstockTypeEnum;
import com.xbongbong.saas.enums.business.InstockEnum;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 吴峰
 * @date 2021/08/09 9:46
 */
@Component
public class SaasInstockHelp {

    @Resource
    private FormDataEsService formDataEsService;

    /**
     * 默认检索全部
     * @param corpid
     * @param refId
     * @param type
     * @return
     * @throws XbbException
     */
    public PaasFormDataEsListVO getInstockListFromEs(String corpid, Long refId, Integer type) throws XbbException {
        return getInstockListFromEs(corpid, refId, type, Arrays.asList(FlowStatusEnum.NOT_NEED_APPROVAL.getType(), FlowStatusEnum.PASS.getType(),
                FlowStatusEnum.APPROVAL_PENDING.getType(), FlowStatusEnum.IN_APPROVAL.getType()), null);
    }

    public PaasFormDataEsListVO getInstockListFromEs(String corpid, Long refId, Integer type, List<Object> flowStatus, List<ConditionsEntityExt> conditionsEntityExts) throws XbbException {
        return getInstockListFromEs(corpid, refId, type, null, null, Arrays.asList(FlowStatusEnum.NOT_NEED_APPROVAL.getType(), FlowStatusEnum.PASS.getType(),
                FlowStatusEnum.APPROVAL_PENDING.getType(), FlowStatusEnum.IN_APPROVAL.getType()), null);
    }

    public PaasFormDataEsListVO getInstockListFromEs(String corpid, Long refId, Integer type, Integer page, Integer pageSize,
                                                     List<Object> flowStatus, List<ConditionsEntityExt> conditionsEntityExts) throws XbbException {
        EsDataDTO esDataDTO = new EsDataDTO();
        esDataDTO.setCorpid(corpid);
        esDataDTO.setBusinessType(InstockTypeEnum.getByCode(type).getStockBusinessType());
        esDataDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
        esDataDTO.setFieldList(null);
        esDataDTO.setPage(page);
        esDataDTO.setPageSize(pageSize);
        esDataDTO.setDel(0);
        List<ConditionsEntityExt> conditionsEntityExtList = getConditionList(type, refId, flowStatus, conditionsEntityExts);

        esDataDTO.setConditionsEntityExtList(conditionsEntityExtList);

        // 按addTime降序排列
        Map<String, String> sortMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        sortMap.put(BusinessConstant.FIELD, ParameterConstant.ADD_TIME);
        sortMap.put(BusinessConstant.ORDER, SortOrder.DESC.toString());
        esDataDTO.setSortMap(sortMap);

        PaasFormDataEsListVO esDataList = formDataEsService.getEsDataList(esDataDTO);
        return esDataList;
    }


    public static List<ConditionsEntityExt> getConditionList(Integer type, Long refId, List<Object> flowStatus, List<ConditionsEntityExt> conditionsEntityExts) {
        List<ConditionsEntityExt> conditionsEntityExtList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (CollectionsUtil.isNotEmpty(conditionsEntityExts)) {
            conditionsEntityExtList = conditionsEntityExts;
        }
        // Type 入库类型
        ConditionsEntityExt typeConditionsEntityExt = new ConditionsEntityExt();
        typeConditionsEntityExt.setAttr(InstockEnum.TYPE.getAttr());
        typeConditionsEntityExt.setValue(Arrays.asList(String.valueOf(type)));
        typeConditionsEntityExt.setSymbol(ConditionEnum.EQUAL.getSymbol());
        typeConditionsEntityExt.setFieldType(InstockEnum.TYPE.getFieldType());
        conditionsEntityExtList.add(typeConditionsEntityExt);
        // refID 关联单据Id
        ConditionsEntityExt refIdConditionsEntityExt = new ConditionsEntityExt();
        refIdConditionsEntityExt.setAttr(InstockEnum.REF_ID.getAttr());
        refIdConditionsEntityExt.setSymbol(ConditionEnum.EQUAL.getSymbol());
        refIdConditionsEntityExt.setFieldType(InstockEnum.REF_ID.getFieldType());
        refIdConditionsEntityExt.setValue(Arrays.asList(String.valueOf(refId)));
        conditionsEntityExtList.add(refIdConditionsEntityExt);

        // flowStatus
        ConditionsEntityExt flowStatusConditionsEntityExt = new ConditionsEntityExt();
        flowStatusConditionsEntityExt.setSymbol(ConditionEnum.IN.getSymbol());
        flowStatusConditionsEntityExt.setAttr("flowStatus");
        flowStatusConditionsEntityExt.setValue(flowStatus);
        flowStatusConditionsEntityExt.setFieldType(FieldTypeEnum.DATAID.getType());
        conditionsEntityExtList.add(flowStatusConditionsEntityExt);
        return conditionsEntityExtList;
    }
}
